import React, { Component } from 'react';
import { Spin } from 'antd';

interface IState {
  loading: boolean;
}

class GlobalLoading extends Component<any, IState> {
  // 无用的构造函数
  // public constructor(props: any) {
  //   super(props);
  //   // GlobalLoading.show = GlobalLoading.show.bind(this);
  //   // GlobalLoading.hide = GlobalLoading.hide.bind(this);
  // }
  //   this.state = {  ts中定义state不能在constructor里定义
  //   loading: false,
  // };

  // readonly 和 Readonly 只用一个就可以，只读的意思，或者两个都留下来
  public readonly state: Readonly<IState> = {
    loading: false,
  };

  public show = () => {
    this.setState({
      loading: true,
    });
  };

  public hide = () => {
    this.setState({
      loading: false,
    });
  };

  public render() {
    const { loading } = this.state;
    const { children } = this.props;
    return <Spin spinning={loading}>{children}</Spin>;
  }
}

export default GlobalLoading;
